SourceForge.net Logo BNF for Java: Project Home Page

The "Backus-Naur Form" (BNF) is a simple yet powerful meta-language. It is a context-free grammar that defines syntax rules in terms of terminal characters (the content of the source text) and non-terminal elements (the syntax of the source language). BNF supports alternative definitions and recursion.

Extended BNF conforms to the International Standard ISO-14977. The improved language is expressive and easy to use.

BNF for Java implements Extended BNF as a working compiler and parser, providing command-line tools, as well as the complete Java API. BNF for Java implements context, and allows you to add your own powerful extensions, such as custom code generation, or database lookup during parsing.

The BNF for Java Project, hosted on SourceForge, is an open-source, community-based team project. The goal is to deliver this useful technology to the world's community of programmers.

About the BNF for Java project

Project Documentation

Examples using BNF

Hello World! A Greeting for the BNF Grammar

(* Here is the phrase structure for our greeting  *)
hello world = greeting word,        (* an introductory word with no meaning *)
              " ",                  (* a space                              *)
              any name,             (* see below                            *)
              "!";                  (* phrase terminator                    *)

(* two ways to spell it: *)
greeting word = 'H','e','l','l','o'  (* this exact spelling *)
              | 'H','i';             (* | or this           *)

(* What's in a name? Nobody knows! So we declare our own rule:  *)
any name = capital letter,        (* one capital,                               *)
           { small letter };      (* followed by { zero or more small letters } *)

capital letter = 'A' | 'B' | 'C' | 'D' | 'E' | 'F' | 'G' | 'H' | 'I'  (* any one of these terminals *)
               | 'J' | 'K' | 'L' | 'M' | 'N' | 'O' | 'P' | 'Q' | 'R'  (* is a "capital letter"      *)
               | 'S' | 'T' | 'U' | 'V' | 'W' | 'X' | 'Y' | 'Z';
small letter = 'a' | 'b' | 'c' | 'd' | 'e' | 'f' | 'g' | 'h' | 'i'  (* any one of these terminals *)
             | 'j' | 'k' | 'l' | 'm' | 'n' | 'o' | 'p' | 'q' | 'r'  (* is a "small letter"        *)
             | 's' | 't' | 'u' | 'v' | 'w' | 'x' | 'y' | 'z';
Apply the above grammar to these source samples:
source string pass reason
Hello World! pass correct syntax
Hi Mother! pass really the same syntax
Hello mom! fail name must start with a Capital
Hello Mother fail phrase needs a "!" terminator
Hi Mother dearest! fail only one name is allowed

A Compiler Quiz

Writing a compiler takes twice as much effort as coding a script.
And then, writing a compiler-compiler is even more trouble!
So, why would any sane programmmer dedicate so much effort into parser technology?
Is it: (select one)

  1. We like working twice as much as the programmer in the next cubicle.
  2. We have discovered an easy algorithm to solve every programming problem ever dreamed of.
  3. We get confused by all the varieties of text, created by those other applications.
  4. It's a nice way to ask our mother, "Hello Mom! Please pass the coffee."

    Valid XHTML 1.1!     Valid CSS!

Our "digits.com" web counter: Web Counter